package reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * @author dell
 * @category 动态代理
 */
public class HelloProxy implements InvocationHandler {

	private Object target;

	//绑定代理目标
	public Object bind(Object target) {
		this.target = target;

		Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
				this);
		return proxy;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO 代理的逻辑
		System.err.println("123");
		Object obj = method.invoke(target, args);
		System.err.println("321");
		return obj;
	}

	public static void main(String[] args)
			throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		HelloProxy helloProxy = new HelloProxy();
		HelloServiceImpl helloServiceImpl = (HelloServiceImpl) Class.forName("springStu.reflect.HelloServiceImpl").newInstance();
		HelloService proxy = (HelloService)helloProxy.bind(helloServiceImpl);
		proxy.sayHello("asda");
	}

}
